package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/carBrandModel")
public class CarBrandModelController extends BaseController{
	
	@RequestMapping(value="/brandList",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String brandList(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag='0'";
		//搜索参数
		if(request.getParameter("recommend")!=null && !request.getParameter("recommend").toString().trim().equals("")){
			where+=" and recommend = ? ";
			whereParamList.add(request.getParameter("recommend"));
		}
		
		if(request.getParameter("first_letter")!=null && !request.getParameter("first_letter").toString().trim().equals("")){
			where+=" and first_letter = ? ";
			whereParamList.add(request.getParameter("first_letter"));
		}
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.carBrand+" "+where+" order by position asc limit 0,100", whereParamList.toArray());

		//拼装返回的数据
		data.put("data", dataList.toArray());
		
		return WebUtil.resultString(data,request);
		
	}
	
	@RequestMapping(value="/categoryModelList",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String categoryModelList(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag='0'";
		
		
		if(request.getParameter("pid")!=null && !request.getParameter("pid").toString().trim().equals("")){
			where+=" and pid = ? ";
			whereParamList.add(request.getParameter("pid"));
		}
		
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.carCategory+" "+where+" order by position asc limit 0,100", whereParamList.toArray());
		
		if(dataList!=null) {
			for(int i=0;i<dataList.size();i++) {
				Map carCategory=(Map)dataList.get(i);
				List modelList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.carModel+" where brand_id=? order by position asc limit 0,200", new Object[] {carCategory.get("id")});
				
				carCategory.put("sub", modelList);
				
			}
		}
		
		
		//拼装返回的数据
		data.put("data", dataList.toArray());
		
		return WebUtil.resultString(data,request);
	}
	
	@RequestMapping(value="/yearList",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String yearList(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag='0'";
		
		
		if(request.getParameter("pid")!=null && !request.getParameter("pid").toString().trim().equals("")){
			where+=" and pid = ? ";
			whereParamList.add(request.getParameter("pid"));
		}
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.carYear+" "+where+" order by position asc limit 0,100", whereParamList.toArray());

		//拼装返回的数据
		data.put("data", dataList.toArray());
		
		return WebUtil.resultString(data,request);
	}
	
	@RequestMapping(value="/styleList",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String styleList(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag='0'";
		
		
		if(request.getParameter("pid")!=null && !request.getParameter("pid").toString().trim().equals("")){
			where+=" and pid = ? ";
			whereParamList.add(request.getParameter("pid"));
		}
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,name from "+DBTable.carStyle+" "+where+" order by position asc limit 0,100", whereParamList.toArray());

		//拼装返回的数据
		data.put("data", dataList.toArray());
		
		return WebUtil.resultString(data,request);
	}
	
	
}
